<?php
namespace app\admin\controller;
use \think\Controller;
use think\Request;
use \think\Session;
class Hold extends Controller
{
    public function _initialize(){
        if(!isLogin()){
            $this->redirect('/trade/login');
        }
    }
    public function index(Request $request)
    {
        //当前登录用户的id
        $id=Session::get('trade_user_id');
        $query=$request->param();
        $list=null;
        $account=null;
        $user=null;

        if(count($query)>0) {

            //只有在有参数时查询
            $account=$request->param('account') ? $request->param('account') : 'not null';
            $user=$request->param('user') ? $request->param('user') : 'not null';

            if ($id == 1) {
                //超级管理员
                $list = db('hold')->alias('a')
                    ->field('a.id,a.account,a.user_name,a.goods,a.type,a.num,a.price,a.bail_value,b.custom_name')
                    ->join('__ADD_NAME__ b', 'a.account = b.account', 'left')
                    ->where('b.custom_name',trims($user))
                    ->where('a.account',trims($account))
                    ->order('a.id')
                    ->paginate(20,false,['query'=>$query]);
            } else {
                //代理用户

                //查询旗下的子代理
                $childId = db('user')->where('pid', $id)->column('id');
                $childId[] = $id;

                $list = db('hold')->alias('a')
                    ->field('a.id,a.account,a.user_name,a.goods,a.type,a.num,a.price,a.bail_value,b.custom_name')
                    ->join('__ADD_NAME__ b', 'a.account = b.account', 'left')
                    ->where('a.pid', 'in', $childId)
                    ->where('b.custom_name',trims($user))
                    ->where('a.account',trims($account))
                    ->order('a.id')
                    ->paginate(20,false,['query'=>$query]);
            }
        }

        $this->assign('list',$list);
        $this->assign('account',$request->param('account'));
        $this->assign('user',$request->param('user'));
        return $this->fetch('list');
    }


}
